package com.enjoy.xbase.nest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;

public class ChildView extends FrameLayout {
    private int mLastX;
    private int mLastY;
    private int mLastX1;
    private int mLastY1;
    private NestListener nestListener;
    private ParentScrollView parentView;


    public ChildView(Context context) {
        super(context);
    }

    public ChildView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChildView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setNestListener(NestListener nestListener) {
        this.nestListener = nestListener;
    }

    public void setParentView(ParentScrollView parentView) {
        this.parentView = parentView;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                //不让父View拦截
                mLastX = (int) ev.getX();
                mLastY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = (int) (ev.getX() - mLastX);
                int deltaY = (int) (ev.getY() - mLastY);
                mLastX = (int) ev.getX();
                mLastY = (int) ev.getY();
                if (nestListener != null && nestListener.allowParent(deltaX, deltaY)) {
                    //让父View拦截
                    if (parentView != null) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                       // parentView.requestDisallowInterceptTouchEvent(true);
                    }
                    return false;
                }else{
                }
        }
        return super.dispatchTouchEvent(ev);
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getActionMasked()){
            case MotionEvent.ACTION_MOVE:
                int deltaX = (int) (ev.getX() - mLastX1);
                int deltaY = (int) (ev.getY() - mLastY1);
                if (nestListener != null && nestListener.allowParent(deltaX, deltaY)) {
                    //让父View拦截
                    if (parentView != null) {
                        //getParent().requestDisallowInterceptTouchEvent(false);
                        // parentView.requestDisallowInterceptTouchEvent(true);
                    }
                    return false;
                }
                break;
        }
        mLastX1 = (int) ev.getX();
        mLastY1 = (int) ev.getY();
        return super.onTouchEvent(ev);
    }
}